package com.boot.mgt.controller.app

import com.boot.core.result.ResultTo
import com.boot.mgt.dao.mysql.TaAccidentReportMapper
import com.boot.mgt.service.TaAccidentReportService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import javax.servlet.http.HttpServletRequest

/**
 * 报警管理接口
 */
@RestController
@RequestMapping("/app/report")
class TaAppAccidentReportController {

    @Autowired
    private lateinit var accidentReportService: TaAccidentReportService

    @Autowired
    protected lateinit var accidentReportMapper: TaAccidentReportMapper

    /**
     *报警信息列表
     */
    @GetMapping("/list")
    fun list(
        request: HttpServletRequest,
        @RequestParam("pageIndex") page_index: Int?, page_size: Int?
    ): ResultTo {
        val map = hashMapOf<String, Any>()
        request.parameterMap?.forEach { t, u -> map[t] = u[0] }
        map["isWeb"] = false
        return accidentReportService.list(page_index, page_size, map)
    }

    /**
     *报警信息列表
     */
    @GetMapping("/listRpc")
    fun listRpc(
        map: HashMap<String, Any>,
        @RequestParam("pageIndex") page_index: Int?, page_size: Int?
    ): ResultTo {
        map["isWeb"] = false
        return accidentReportService.listRpc(page_index, page_size, map)
    }

    /**
     * 报警信息详情
     */
    @GetMapping("/detail")
    fun detail(id: Int): ResultTo {
        return accidentReportService.detail(id, true)
    }

    /**
     * 关联数据
     */
    @GetMapping("/{reportId}")
    fun correlation(@PathVariable reportId: Int): ResultTo {
        val taAccidentReport = accidentReportMapper.selectByPrimaryKey(reportId)
        val correlationDate = accidentReportService.correlationDate(taAccidentReport.acceptTime!!)
        val selectOracleCorrelation = accidentReportService.selectOracleCorrelation(correlationDate)
        return accidentReportService.correlation(correlationDate, selectOracleCorrelation)
    }

    /**
     * 关联数据，根据事故上报时间和所属大队，匹配六合一的事故列表
     */
    @PostMapping("/correlationRpc")
    fun correlationRpc(reportId: Int, org_num: String?): ResultTo {
        val taAccidentReport = accidentReportMapper.selectByPrimaryKey(reportId)
        val correlationDate = accidentReportService.correlationDate(taAccidentReport.acceptTime!!)
        return accidentReportService.selectOracleCorrelationRpc(correlationDate, org_num)
    }

    /**
     * 确认关联数据
     */
    @GetMapping("/{reportId}/{sgbh}")
    fun checkCorrelation(@PathVariable reportId: Int, @PathVariable sgbh: String): ResultTo {
        val oracleTAcdFile = accidentReportService.selectOracleBySgbh(sgbh)
        return accidentReportService.checkCorrelation(reportId, oracleTAcdFile)
    }

    /**
     * 确认关联数据
     */
    @GetMapping("/checkCorrelationRpc")
    fun checkCorrelationRpc(reportId: Int, sgbh: String): ResultTo {
        val oracleTAcdFile = accidentReportService.selectOracleBySgbh(sgbh)

        return accidentReportService.checkCorrelation(reportId, oracleTAcdFile)
    }

    /**
     * 根据编号获取oracle详细信息
     */
    @GetMapping("/oracleFile/{sgbh}")
    fun selectOracleById(@PathVariable sgbh: String): ResultTo {
        val tAcdFile = accidentReportService.selectOracleBySgbh(sgbh)
        return ResultTo().setData("oracleFile", tAcdFile)
    }
}